package com.tanhua.app.controller;

import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.app.service.SmallVideosService;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService smallVideosService;

    //上传视频
    @PostMapping
    public ResponseEntity uploadSmallVideos(MultipartFile videoThumbnail,MultipartFile videoFile) throws IOException {
        smallVideosService.uploadSmallVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }

    //小视频列表
    @GetMapping
    public ResponseEntity samllVideoList(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pagesize){
        PageResult<VideoVo> pr = smallVideosService.samllVideoList(page,pagesize);
        return ResponseEntity.ok(pr);
    }

    //小视频点赞
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable String id){
        smallVideosService.like(id);
        return ResponseEntity.ok(null);
    }

    //小视频取消点赞
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable String id){
        smallVideosService.dislike(id);
        return ResponseEntity.ok(null);
    }

    //小视频关注
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable String uid){
        smallVideosService.focus(uid);
        return ResponseEntity.ok(null);
    }

    //小视频取消关注
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable String uid){
        smallVideosService.unFocus(uid);
        return ResponseEntity.ok(null);
    }

    //评论列表
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   @PathVariable String id){
        PageResult<CommentVo> pr = smallVideosService.comments(page,pagesize,id);
        return ResponseEntity.ok(pr);
    }

    //评论发布
    @PostMapping("/{id}/comments")
    public ResponseEntity saveComments(@PathVariable String videoId, @RequestBody Map map){
        String comment = (String) map.get("comment");
        smallVideosService.saveComment(videoId,comment);
        return ResponseEntity.ok(null);
    }
}
